package com.cictec.middleware.gps.caculate;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.alibaba.fastjson.JSON;
import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.commons.utils.DateUtils;
import com.cictec.middleware.commons.utils.PMSUtils;
import com.cictec.middleware.commons.utils.PropertiesUtils;
import com.cictec.middleware.core.entity.BusDriverSignIn;
import com.cictec.middleware.core.entity.BusLineStation;
import com.cictec.middleware.core.entity.DeparterSchedule;
import com.cictec.middleware.core.entity.LineDeviatePlan;
import com.cictec.middleware.gps.engine.BusInstance;
import com.cictec.middleware.gps.engine.TimingPoint;
import com.cictec.middleware.gps.engine.caculate.entity.BusTrip;
import com.cictec.middleware.gps.engine.service.DataStorageToRedisService;
import com.cictec.middleware.gps.service.BusDriverService;
import com.cictec.middleware.gps.service.BusLineService;
import com.cictec.middleware.gps.service.DeviateService;
import com.cictec.middleware.gps.service.ScheduleService;

/**
 * 趟次计算
 * 
 * @author ql
 *
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
@Repository
public class BusTripEngine {

	public static Logger logger = LoggerFactory.getLogger(BusTripEngine.class);
	
	@Autowired
	private DataStorageToRedisService dataStorageToRedisService;
	@Autowired
	private ScheduleService scheduleService;
	@Autowired
	private BusLineService busLineService;
	@Autowired
	private BusDriverService busDriverService;
	@Autowired
	private DeviateService deviateService;
	
	
	/**
	 * 获取上一个完成的趟次信息
	 * @param devCode
	 * @param endTime
	 * @return
	 */
	public BusTrip getLastBusTrip(String devCode, Date endTime){
		
		List<BusTrip> tripList = getTrip(DateUtils.getStartDateTime(endTime), DateUtils.getEndDateTime(endTime), devCode);
		BusTrip busTrip = tripList.size() == 0 ? null : tripList.get(tripList.size() - 1);
		
		return busTrip;
		
	}
	
	/**
	 * redis中的进出站进行趟次计算
	 * @param startDate
	 * @param endDate
	 * @param plate
	 * @return
	 */
	private List<BusTrip> getTrip(Date startDate, Date endDate, String plate) {
		List<TimingPoint> points = dataStorageToRedisService.getEnterOutFromRedis(plate, startDate, endDate);
		return getTrip(points);
	}
	
	/**
	 * 根据进出站点 计算趟次信息
	 * @param points
	 * @return
	 */
	public List<BusTrip> getTrip(List<TimingPoint> points) {
		
		// 获取进出站数据
		List<TimingPoint> timingPoints = points;
//		/* ********************** 过滤重复数据【站序、时间】start*****************************************************/
//		Map<String, TimingPoint> map = new HashMap<String, TimingPoint>();
//		for (TimingPoint TimingPoint : timingList) {
//			long time = TimingPoint.getTime().getTime();
//			String lineType = TimingPoint.getAttribute(Constants.LINE_TYPE);
//			String seq = TimingPoint.getAttribute(Constants.LINE_STATION_SEQ_ORG);
//			String stat = TimingPoint.getAttribute(Constants.BUS_ENTER_OUT_SATION);
//			String key = time + lineType+seq+stat;
//			map.put(key, TimingPoint);
//		}
//		timingList = new ArrayList<TimingPoint>(map.values());
//		/* ********************** 过滤重复数据【站序、时间】end*****************************************************/
		
		/* ********************** 过滤进出站【触发类型】 start *****************************************************/
		/** 【原始】进出站触发类型 触发类型 0：表示自动触发 1：表示手动触发 2：表示调试数据 3:wifi 报站类型  */
		// 趟次计算，进出站触发类型过滤【0：不区分触发类型、1：自动触发、2：手动触发】
		String enterOutFilter = PropertiesUtils.getString("system.parameter.trip.enterout.filter", "0");
		if(!enterOutFilter.equals("0")){
			List<TimingPoint> filterList = new ArrayList<TimingPoint>();
			for (TimingPoint timingPoint : points) {
				String triggerType = timingPoint.getStationEnterOutTrigger();
				if(PMSUtils.isNotEmpty(triggerType)){
					if(enterOutFilter.equals("1") && triggerType.equals("0")){
						filterList.add(timingPoint);// 只添加自动触发的
					}else if(enterOutFilter.equals("2") && triggerType.equals("1")){
						filterList.add(timingPoint);// 只添加手动触发的
					}
				}else{
					// 老数据没有保存触发类型，所有默认全部
					filterList.add(timingPoint);
				}
			}
			
			timingPoints = filterList;
		}
		/* ********************** 过滤进出站【触发类型】 end *****************************************************/
		
		
		// 按照时间 排序
		timingPoints.sort(new Comparator<TimingPoint>() {
			@Override
			public int compare(TimingPoint o1, TimingPoint o2) {
				int result = o1.getTime().compareTo(o2.getTime());
				return result;
			}
		});
		

		List<BusTrip> busTrips = new ArrayList<BusTrip>();
		if (timingPoints.size() > 3) {
			
			Integer lineBfType = busLineService.getLineBfType(timingPoints.get(0).getLineUuid());
			busTrips = cpBusTrip(timingPoints, lineBfType);
		}
		
		
		for (BusTrip busTrip : busTrips) {
			DeparterSchedule ds = scheduleService.getDeparterSchedule(busTrip.getLineid(), busTrip.getLinetype(), busTrip.getDoduuid(), busTrip.getStartTime(), 0, false);
			if (ds != null) {
				busTrip.setCurrentShift(ds.getBrsdShift());
				busTrip.setDepartureTime(ds.getDepartureTime());
			}else{
				// 查不出来，默认开始时刻
				busTrip.setDepartureTime(busTrip.getStartTime());
			}
		}
		return busTrips;
	}
	
	/**
	 * 根据传入的 时序点，统计趟次
	 * @param timingPoints
	 * @param lineBfType	线路发车类型（0单向 ，1双向，2环线）
	 * @return
	 */
	private List<BusTrip> cpBusTrip(List<TimingPoint> timingPoints, int lineBfType) {

		
		List<BusTrip> busTrips = new ArrayList<BusTrip>();

		List<List<TimingPoint>> tripList = null;
		if(lineBfType == 1){
			// 
			/* ******************* 按照上下行 切分进出站信息 start *****************************************/
			tripList = getTripTimingListByLineType(timingPoints);
			/* ******************* 按照上下行 切分进出站信息 end *****************************************/

			busTrips = getBusTripsByLineType(tripList);
			
		}else if(lineBfType == 0 || lineBfType == 2)	{
			// 单向、环线
			tripList = getTripTimingListByStationSeq(timingPoints, lineBfType);
			
			busTrips = getBusTripsBySeq(tripList, lineBfType);
		}

		// 设置 趟次序号（实际趟次序号）
		for (int i = 0; i < busTrips.size(); i++) {
			busTrips.get(i).setTripnumber(i + 1);
		}

		return busTrips;
	}
	
	private List<BusTrip> getBusTripsByLineType(List<List<TimingPoint>> tripList){
		List<BusTrip> busTrips = new ArrayList<BusTrip>();
		
		for (int i = 0; i < tripList.size(); i++) {

//			System.out.println(DateUtils.formatDateTime(tripList.get(i).get(0).getTime()));
			
			// 如果是一个有效的趟次数据
			if (isTrip(tripList.get(i))) {

				busTrips.add(createTrip(tripList.get(i)));
			} else {
				/**
				 * 判定如下： 
				 * 1、如果i == 0， 【第一趟】忽略掉 
				 * 2、 如果 i > 0 && i < tripList.size() - 1，【中间趟次】 合并 i -1 与 i + 1 既 两个通向的趟次合并 
				 * 3、如果 i == tripList.size() - 1 【最后一趟】, 忽略掉
				 * 【注意】【趟次序号顺序连续累加才算】
				 * 
				 */
				// 不是有效趟次数据，合并前一趟次，和后一趟次
				if (i == 0 || i == (tripList.size() - 1)) {
					// 端点数据不处理
				} else {
					List<TimingPoint> preTrip =  tripList.get(i - 1);
					List<TimingPoint> currentTrip =  tripList.get(i);
					List<TimingPoint> afterTrip =  tripList.get(i + 1);
					
					String preSeqStart = preTrip.get(preTrip.size() -1 ).getStationLineSeqOrg();
					String preSeqEnd = preTrip.get(preTrip.size() -1 ).getStationLineSeqOrg();
					String afterSeqStart = afterTrip.get(0).getStationLineSeqOrg();
					
					int preStartSeq = Integer.parseInt(preSeqStart);
					int preEndSeq = Integer.parseInt(preSeqEnd);
					int afterStartSeq = Integer.parseInt(afterSeqStart);
					
					if(preEndSeq <= afterStartSeq){
						// 如果前一趟和后一趟站序 递增
						String curretnLineType = currentTrip.get(0).getStationLineTypeOrg();
						String preLineType = preTrip.get(0).getStationLineTypeOrg();
						String afterLineType = afterTrip.get(0).getStationLineTypeOrg();
						
						
						
//						System.out.println("preLineType:"+ preLineType
//								+ "\tcurretnLineType:" + curretnLineType
//								+ "\tafterLineType:" + afterLineType
//								);
						
						if ((!isTrip(preTrip) && isTrip(afterTrip))) {
							if(preLineType.equals(afterLineType)){
								List<TimingPoint> newTripPointList = new ArrayList<TimingPoint>();
								newTripPointList.addAll(preTrip);
								newTripPointList.addAll(afterTrip);
								
//								busTrips.remove(busTrips.size() - 1);// 移除最后一趟
								busTrips.add(createTrip(newTripPointList)); // 重新添加最后一趟
								i++;
							}
							
						}else if(isTrip(preTrip) && !isTrip(afterTrip)){
							
						}else if(!isTrip(preTrip) && !isTrip(afterTrip)){
							
							if(preLineType.equals(afterLineType)){
								List<TimingPoint> newTripPointList = new ArrayList<TimingPoint>();
								newTripPointList.addAll(preTrip);
								newTripPointList.addAll(afterTrip);
								
								if(isTrip(newTripPointList)){
//									busTrips.remove(busTrips.size() - 1);// 移除最后一趟
									busTrips.add(createTrip(newTripPointList)); // 重新添加最后一趟
									i++;
								}
							}
							
						}
					}
				}
			}
		}
		
		return busTrips;
	}
	
	private List<BusTrip> getBusTripsBySeq(List<List<TimingPoint>> tripList, int lineBfType){
		
		List<BusTrip> busTrips = new ArrayList<BusTrip>();
		
		for (int i = 0; i < tripList.size(); i++) {

//			System.out.println(DateUtils.formatDateTime(tripList.get(i).get(0).getTime()));
			
			// 如果是一个有效的趟次数据
			if (isTrip(tripList.get(i))) {

				busTrips.add(createTrip(tripList.get(i)));
			} else {
				/**
				 * 判定如下： 
				 * 1、如果i == 0， 【第一趟】忽略掉 
				 * 2、 如果 i > 0 && i < tripList.size() - 1，【中间趟次】 合并 i -1 与 i + 1 既 两个通向的趟次合并 
				 * 3、如果 i == tripList.size() - 1 【最后一趟】, 忽略掉
				 * 【注意】【趟次序号顺序连续累加才算】
				 * 
				 */
				// 不是有效趟次数据，合并前一趟次，和后一趟次
				if (i == 0 || i == (tripList.size() - 1)) {
					// 端点数据不处理
				} else {
					List<TimingPoint> preTrip =  tripList.get(i - 1);
					List<TimingPoint> currentTrip =  tripList.get(i);
					List<TimingPoint> afterTrip =  tripList.get(i + 1);
					
					String preSeqStart = preTrip.get(preTrip.size() -1 ).getStationLineSeqOrg();
					String preSeqEnd = preTrip.get(preTrip.size() -1 ).getStationLineSeqOrg();
					String afterSeqStart = afterTrip.get(0).getStationLineSeqOrg();
					
					int preStartSeq = Integer.parseInt(preSeqStart);
					int preEndSeq = Integer.parseInt(preSeqEnd);
					int afterStartSeq = Integer.parseInt(afterSeqStart);
					
					if(preEndSeq <= afterStartSeq){
						
//						System.out.println("preLineType:"+ preLineType
//								+ "\tcurretnLineType:" + curretnLineType
//								+ "\tafterLineType:" + afterLineType
//								);
						
						if ((!isTrip(preTrip) && isTrip(afterTrip))) {
							if(preStartSeq < afterStartSeq){
//							if(preLineType.equals(afterLineType)){
								List<TimingPoint> newTripPointList = new ArrayList<TimingPoint>();
								newTripPointList.addAll(preTrip);
								newTripPointList.addAll(afterTrip);
								
//								busTrips.remove(busTrips.size() - 1);// 移除最后一趟
								busTrips.add(createTrip(newTripPointList)); // 重新添加最后一趟
								i++;
							}
							
						}else if(isTrip(preTrip) && !isTrip(afterTrip)){
							
						}else if(!isTrip(preTrip) && !isTrip(afterTrip)){
							
							if(preStartSeq < afterStartSeq){
								List<TimingPoint> newTripPointList = new ArrayList<TimingPoint>();
								newTripPointList.addAll(preTrip);
								newTripPointList.addAll(afterTrip);
								
								if(isTrip(newTripPointList)){
//									busTrips.remove(busTrips.size() - 1);// 移除最后一趟
									busTrips.add(createTrip(newTripPointList)); // 重新添加最后一趟
									i++;
								}
							}
							
						}
					}
				}
			}
		}

		if(lineBfType == 0){
			// 单向设置方向为上行
			for (BusTrip busTrip : busTrips) {
				busTrip.setLinetype("1");
			}
		}
		
		return busTrips;
	}
	
	/**
	 * 按照上下行切分趟次
	 * @param TimingPoints
	 * @return
	 */
	private List<List<TimingPoint>> getTripTimingListByLineType(List<TimingPoint> TimingPoints){
		/* ******************* 按照上下行 切分进出站信息 start *****************************************/
		List<List<TimingPoint>> tripList = new ArrayList<List<TimingPoint>>(); // 每一趟次的 进出站信息
		List<TimingPoint> tripPointList = null; 								 // 当前趟次的 进出站信息
		String tmpLineType = null; // 当前趟次的 线路类型
		String tmpLineUuid = null;
		for (TimingPoint timingPoint : TimingPoints) {
			String lineType = timingPoint.getStationLineTypeOrg();
			String lineUuid = timingPoint.getLineUuid();
			if (lineType == null) {
				continue;
			}
			
//			String lineName = TimingPoint.getAttribute(Constants.MSG_INFO_POSITION_LINE_NAME);
//			String time = TimingPoint.getAttribute(Constants.MSG_INFO_POSITION_GATHER_TIME);
//			if(time.indexOf("16:08") > 0){
//				System.out.println(time);
//			}
//			String seq = TimingPoint.getAttribute(Constants.LINE_STATION_SEQ_ORG);
//			System.out.println(lineName + "\t" + lineType + "\t" + seq + "\t " + time.substring(11, 19));

			if (tripPointList == null) {
				tripPointList = new ArrayList<TimingPoint>();
				tripPointList.add(timingPoint);
				
				tripList.add(tripPointList);
			} else {
				// 判定是否是同一趟次
				if (tmpLineType.equals(lineType) && tmpLineUuid.equals(lineUuid)) {
					tripPointList.add(timingPoint);
				} else {
					
					tripPointList = new ArrayList<TimingPoint>();
					tripPointList.add(timingPoint);
					tripList.add(tripPointList);
					
//					tripList.add(tripPointList);
//					tripPointList = new ArrayList<TimingPoint>();
//					tripPointList.add(TimingPoint);

				}
			}
			tmpLineType = lineType;
			tmpLineUuid = lineUuid;
		}
		/* ******************* 按照上下行 切分进出站信息 end *****************************************/
		return tripList;
	}
	
	
	/**
	 * 按照站序切分趟次
	 * @param timingPoints
	 * @return
	 */
	private List<List<TimingPoint>> getTripTimingListByStationSeq(List<TimingPoint> timingPoints, int lineBfType){
		
		int uplast = 0;
		if(lineBfType == 0){
			// 单向发车的时候 补全方法
			BusLineStation ls = busLineService.getLineStationLast(timingPoints.get(0).getLineUuid(), "1");
			uplast = ls.getLsSequence();
		}
		
		List<List<TimingPoint>> tripList = new ArrayList<List<TimingPoint>>(); // 每一趟次的 进出站信息
		
		List<TimingPoint> tripPointList = null; 						 // 当前趟次的 进出站信息
		int currentSeq = 0;
		String tmpLineUuid = null;
		for (TimingPoint timingPoint : timingPoints) {
			String seq = timingPoint.getStationLineSeqOrg();
			String lineUuid = timingPoint.getLineUuid();
			String lineType = timingPoint.getStationLineTypeOrg();
			if(PMSUtils.isEmpty(seq)){
				continue;
			}
			
			Integer cseq = null;
			if(lineBfType == 0){
				cseq = Integer.parseInt(seq) +  (lineType.equals(Constants.VAL_LINE_UP) ? 0 : uplast);
			}else{
				cseq = Integer.parseInt(seq);
			}
//			System.out.println(lineType + "\t" + cseq);
			
			if(currentSeq == 0){
				tripPointList = new ArrayList<TimingPoint>(); 
				tripPointList.add(timingPoint);
				tripList.add(tripPointList);
				

			}else{
				if(currentSeq <= cseq && tmpLineUuid.equals(lineUuid)) {
					tripPointList.add(timingPoint);
				}else{
//					String trip = "";
//					for (TimingPoint TimingPoint1 : tripPointList) {
//						String seqa = TimingPoint1.getAttribute(Constants.LINE_STATION_SEQ_ORG);
//						trip += trip.length() ==0 ? seqa : "," + seqa	;
//					}
//					System.out.println(trip);
					tripPointList = new ArrayList<TimingPoint>();
					tripPointList.add(timingPoint);
					tripList.add(tripPointList);
					
				}
			}
			
			tmpLineUuid = lineUuid;
			currentSeq = cseq;
		}
		
//		for (List<TimingPoint> list : tripList) {
//			String trip = "";
//			for (TimingPoint TimingPoint : list) {
//				String seq = TimingPoint.getAttribute(Constants.LINE_STATION_SEQ_ORG);
//				trip += trip.length() ==0 ? seq : "," + seq	;
//			}
//			System.out.println(trip);
//		}
//		System.out.println("size: " + tripList.size());
		return tripList;
	}
	
	/**
	 * 是否满足一趟的要求
	 * 
	 * @param tripPointList
	 * @return
	 */
	private boolean isTrip(List<TimingPoint> tripPointList) {


		if (tripPointList.size() < 3) { // 至少经过2站
			
			logger.debug("进出站数 {} 小于 3 站，不够一趟 {}:", tripPointList.size(), JSON.toJSONString(tripPointList));
			return false;
		}
		
		if(!PMSUtils.isEmpty(PropertiesUtils.getString("system.parameter.trip.rate"))){
			double tripRate = PropertiesUtils.getDoubleValue("system.parameter.trip.rate", 0.5);
			
			TimingPoint firstPoint = tripPointList.get(0);
			String lineUuid = firstPoint.getLineUuid();
			String lineType = firstPoint.getStationLineTypeOrg();
			
			// 过滤重复站点
			Map tmp = new HashMap(); 
			for (TimingPoint timingPoint : tripPointList) {
				String seq = timingPoint.getStationLineSeqOrg();
				if(!tmp.containsKey(seq)){
					tmp.put(seq, timingPoint);
				}
			}
			Integer tripSeqSize = tmp.size();
			
			Integer lineBfType = busLineService.getLineBfType(lineUuid);// 发车类型
			
			List<BusLineStation> list = new ArrayList<BusLineStation>();
			// 发车类型（0单向 ，1双向，2环线）
			if(lineBfType == 0){
				list = busLineService.getLineStations(lineUuid, null);
			}else if(lineBfType == 1){
				list = busLineService.getLineStations(lineUuid, lineType);
			}else if(lineBfType == 2){
				list = busLineService.getLineStations(lineUuid, lineType);
			}
			
			Integer totalSize = list.size();
			
			/* ***********添加偏线计划对趟次的影响 start*****************************************/
			boolean deviateplan = PropertiesUtils.getBoolean("system.parameter.deviateplan", false);
			if(deviateplan){	

				TimingPoint lastPoint = tripPointList.get(tripPointList.size()-1);
				List<LineDeviatePlan> planList = deviateService.getDeviatePlanByTimeSegment(lineUuid, lineType, firstPoint.getTime(), lastPoint.getTime());
				if(planList.size() > 0){
					List<BusLineStation> ls = new ArrayList<BusLineStation>();
					for (LineDeviatePlan dp : planList) {
						int _startSeq = dp.getStartStationSequence();
						int _endSeq = dp.getEndStationSequence();
						String _lineType = dp.getLdpLineType();
						for (BusLineStation lineStation : list) {
							String lsLineType = lineStation.getLsLineType();
							int lsSeq = lineStation.getLsSequence();
							
							if(lsLineType.equals(_lineType)){
								if(_startSeq <= lsSeq && lsSeq <= _endSeq){
									
								}else{
									ls.add(lineStation);
								}
							}
						}
					}
					totalSize = ls.size();
				}
				
			}
			/* ***********添加偏线计划对趟次的影响  end *****************************************/
			
			
//			Integer tripSize = tripPointList.size();
//			double _rate = tripSize.doubleValue() / totalSize.doubleValue();
			double _rate = tripSeqSize / totalSize.doubleValue();
			
			if(_rate < tripRate){
				
				String str = null;
				for (TimingPoint point : tripPointList) {

					String _lineType = point.getAttribute(Constants.LINE_TYPE_ORG);
					String _time = point.getAttribute(Constants.MSG_INFO_POSITION_GATHER_TIME);
					String _enterout = point.getAttribute(Constants.BUS_ENTER_OUT_SATION);
					String _seq = point.getAttribute(Constants.LINE_STATION_SEQ_ORG);
					_seq = _seq + (_enterout.equals("1") ? "【进】":"【出】"); 
//					str = str == null ? ("lineType: " + _lineType + " startTime:" + _time + "startSeq:" + _seq) : (str + ", " + _seq);
					str = str == null ? ("lineType: " + _lineType + " startTime:" + _time + " startSeq:" + _seq) : (str + ", " + " startTime:" + _time + " startSeq:" + _seq);
				}
				
				logger.debug("\n进出站数 {}/{} 小于 设置的百分比{}  不够成一趟:{}", tripSeqSize, totalSize, tripRate, str);
				return false;
			}
		}

		return true;
	}
	
	/**
	 * 生成一个趟次
	 * @param tripPointList	该趟次的进出站数据
	 * @param type
	 * @return
	 */
	private BusTrip createTrip(List<TimingPoint> tripPointList) {
		
 
		/* ******** 过滤首站时间过长的问题 start**************************/
		TimingPoint firstPoint = tripPointList.get(0);
		int firstSeq = Integer.parseInt(firstPoint.getStationLineSeqOrg());
		if(firstSeq == 1){
			
			TimingPoint secondPoint = tripPointList.get(1);
			long secondInterval = DateUtils.secondInterval(firstPoint.getTime(), secondPoint.getTime());
			long secondCha = 25 * 60;	// 第一站第二站点如果时间间隔超过设定阀值
			if(secondInterval > secondCha){
				// 如果两个站点间时间差 大于设置的阀值
				// 方法1：默认开始时间 从第二站向前提前3分钟，固定方式
				// 方法2：获取两个站点间的所有位置信息，取最后一段速度不为0的连续点的第一个时间为开始时间，rate >0的点
				
				// 先按照方法1实现一次
//				tripPointList.remove(0);
				firstPoint.setTime(DateUtils.getDateFromMinute(secondPoint.getTime(), -5));
			}
		}
		
		
		
		/* ******** 过滤首站时间过长的问题  end **************************/
		
		BusTrip busTrip = new BusTrip();
		TimingPoint startPoint = tripPointList.get(0);
		busTrip.setUuid(startPoint.getBusUuid());
		busTrip.setPlate(startPoint.getBusNumber());
		busTrip.setLineid(startPoint.getLineUuid());
		busTrip.setLinetype(startPoint.getStationLineTypeOrg());
		busTrip.setStartTime(startPoint.getTime());
		
		if(!PMSUtils.isEmpty(startPoint.getStationLineSeqOrg())){
			busTrip.setStartseq(Integer.parseInt(startPoint.getStationLineSeqOrg()));
		}else{
			busTrip.setStartseq(Integer.parseInt(startPoint.getStationSeq()));
		}

		TimingPoint endPoint = tripPointList.get(tripPointList.size() - 1);

		busTrip.setEndTime(endPoint.getTime());
		
		if(!PMSUtils.isEmpty(endPoint.getStationLineSeqOrg())){
			busTrip.setEndseq(Integer.parseInt(endPoint.getStationLineSeqOrg()));
		}else{
			busTrip.setEndseq(Integer.parseInt(endPoint.getStationSeq()));
		}

		String busUuid = endPoint.getBusUuid();
		BusDriverSignIn driverSignIn = busDriverService.getSignDriver(busUuid, endPoint.getTime());
		if (null != driverSignIn) {
			busTrip.setDoduuid(driverSignIn.getSignDrvUuid());
			busTrip.setDriverName(PMSUtils.isNull(driverSignIn.getDrvName()));
		}

		return busTrip;
	}
	
}
